A compact cheat sheet of MySQL functions you'll meet in the wild. Most won't be covered in class, this is for your future self when you need them.
Compiled by: K.M. Tanvir âĸ Institute of Statistical Research and Training (ISRT), University of Dhaka
How to use: Browse by category in the table of contents. Every entry has the signature, a one-line description, and a short example using the Sakila sample database, so you can paste and adapt.
Already covered in Lecture 5: CONCAT, UPPER, LOWER, LENGTH, ROUND, CEIL, FLOOR, ABS, YEAR, MONTH, DAY. Those are listed below too for completeness.
For trimming, splitting, padding, searching, and reshaping text values.
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM customer;
SELECT CONCAT_WS(', ', first_name, last_name, email) FROM customer;
SELECT UPPER(first_name), LOWER(email) FROM customer;
SELECT title, CHAR_LENGTH(title) AS chars FROM film;
SELECT TRIM(' hello '); -- 'hello'
SELECT SUBSTRING(title, 1, 10) FROM film; -- first 10 chars
SELECT LEFT(title, 5), RIGHT(title, 3) FROM film;
from with to. Case-sensitive.SELECT REPLACE(email, '@sakilastaff.com', '@isrt.ac.bd') FROM staff;
sub inside s (1-based), or 0 if not found.SELECT title, INSTR(title, 'MAN') FROM film;
SELECT LPAD(customer_id, 5, '0') FROM customer; -- '00007'
SELECT REPEAT('-', 10); -- '----------'
SELECT REVERSE('sakila'); -- 'alikas'
SELECT FORMAT(1234567.891, 2); -- '1,234,567.89'
delim. Negative count counts from the end.SELECT SUBSTRING_INDEX(email, '@', 1) FROM customer; -- the username
SELECT CONCAT(first_name, SPACE(2), last_name) FROM customer;
Rounding, math, randomness, and a few helpers.
SELECT ROUND(amount, 1) FROM payment;
SELECT CEIL(amount), amount FROM payment;
SELECT FLOOR(amount) FROM payment;
TRUNCATE(3.789, 1) â 3.7.SELECT TRUNCATE(amount, 1) FROM payment;
SELECT ABS(amount - 5.00) FROM payment;
SELECT SIGN(amount - 5.00) FROM payment;
SELECT customer_id, MOD(customer_id, 10) FROM customer;
SELECT POWER(2, 10); -- 1024
SELECT SQRT(81); -- 9
SELECT LN(2.718), LOG(10, 1000);
SELECT PI() * 2;
[0, 1). Useful with ORDER BY for random sampling.SELECT title FROM film ORDER BY RAND() LIMIT 5;
SELECT GREATEST(rental_rate, 3.99) FROM film;
a DIV b; remainder: a MOD b or a % b.SELECT 17 DIV 5, 17 MOD 5; -- 3, 2
The most useful set of functions for any real-world database. Sakila has plenty of date columns to practice on (rental_date, payment_date, last_update, etc.).
SELECT NOW(); -- e.g. '2026-05-11 14:30:00'
SELECT CURDATE(); -- '2026-05-11'
SELECT CURTIME(); -- '14:30:00'
SELECT YEAR(rental_date), MONTH(rental_date), DAY(rental_date) FROM rental;
SELECT HOUR(rental_date), MINUTE(rental_date) FROM rental;
SELECT DAYNAME(rental_date) FROM rental; -- 'Tuesday'
SELECT DAYOFWEEK(rental_date), WEEK(rental_date) FROM rental;
SELECT DATE(rental_date) FROM rental; -- drop the time
SELECT DATE_ADD(rental_date, INTERVAL 7 DAY) FROM rental;
SELECT ADDDATE(rental_date, 7) FROM rental;
SELECT DATEDIFF(return_date, rental_date) AS days_kept FROM rental;
SELECT TIMESTAMPDIFF(YEAR, create_date, NOW()) FROM customer; -- account age in years
SELECT DATE_FORMAT(rental_date, '%d-%b-%Y') FROM rental; -- '14-Jun-2005'
SELECT STR_TO_DATE('14-06-2005', '%d-%m-%Y');
SELECT LAST_DAY('2005-06-14'); -- '2005-06-30'
SELECT EXTRACT(YEAR FROM rental_date) FROM rental;
SELECT UNIX_TIMESTAMP(rental_date) FROM rental;
SELECT QUARTER(rental_date), COUNT(*) FROM rental GROUP BY 1;
Tools for turning NULLs into something useful, or for picking between values.
SELECT IFNULL(return_date, 'STILL OUT') FROM rental;
SELECT COALESCE(phone, email, 'no contact') FROM customer;
SELECT NULLIF(phone, '') FROM customer; -- empty string -> NULL
SELECT title, IF(length > 120, 'long', 'short') FROM film;
SELECT title, CASE WHEN length < 60 THEN 'short' WHEN length < 120 THEN 'medium' ELSE 'long' END AS length_bucket FROM film;
Force a value to a specific type, useful when MySQL's automatic coercion gets in your way.
SELECT CAST('2026-05-11' AS DATE);
CONVERT(x USING utf8) also changes character set.SELECT CONVERT(amount, CHAR) FROM payment;
These work on a group of rows rather than a single row. We'll cover them properly in Lecture 7, but here's a head start.
GROUP BY, which is Lecture 7's main topic. Used alone on the whole table, they collapse all rows into one summary row.COUNT(*) counts all rows; COUNT(col) counts rows where col is not NULL.SELECT COUNT(*) FROM film;
SELECT SUM(amount) FROM payment; -- total revenue
SELECT AVG(rental_rate) FROM film;
SELECT MIN(length), MAX(length) FROM film;
SELECT rating, GROUP_CONCAT(title SEPARATOR ', ') FROM film GROUP BY rating;
Little helpers that come up when debugging or writing portable queries.
SELECT DATABASE();
SELECT USER();
SELECT VERSION();
INSERT INTO author (name) VALUES ('New Author'); SELECT LAST_INSERT_ID();